/*************************************************************************
	> File Name: 225.用队列实现栈.c
	> Author: Maureen 
	> Mail: Maureen@qq.com 
	> Created Time: 二  9/ 7 18:37:46 2021
 ************************************************************************/

//算法思想：一个队列实现栈
//新元素入队，并将队列中该元素前的元素依次出队并入队

typedef  struct tagLinkNode {
    int val;
    struct tagLinkNode *next;
} LinkNode;

typedef struct {
    LinkNode *top;
} MyStack;

/** Initialize your data structure here. */

MyStack* myStackCreate() {
    MyStack *stk = calloc(1, sizeof(MyStack));
    return stk;
}

/** Push element x onto stack. */
void myStackPush(MyStack* obj, int x) {
    LinkNode *node = (LinkNode *)malloc(sizeof(LinkNode));
    node->val = x;
    node->next = obj->top;
    obj->top = node;
    return ;
}

/** Removes the element on top of the stack and returns that element. */
int myStackPop(MyStack* obj) {
    if (obj->top == NULL) return -1;
    LinkNode *node = obj->top;
    int data = node->val;
    obj->top = node->next;
    free(node);
    return data;
}

/** Get the top element. */
int myStackTop(MyStack* obj) {
    return obj->top ? obj->top->val : -1;
}

/** Returns whether the stack is empty. */
bool myStackEmpty(MyStack* obj) {
    return (obj->top) == NULL;
}

void myStackFree(MyStack* obj) {
    while (obj->top != NULL) {
        LinkNode *node = obj->top;
        obj->top = node->next;
        free(node);
    }
    return ;
}

/**
 * Your MyStack struct will be instantiated and called as such:
 * MyStack* obj = myStackCreate();
 * myStackPush(obj, x);

 * int param_2 = myStackPop(obj);

 * int param_3 = myStackTop(obj);

 * bool param_4 = myStackEmpty(obj);

 * myStackFree(obj);
*/
